﻿using Host;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AXImServer
{
    public class UserComponent : IComponents
    {
        public IUserService UserService { get; set; }

        private CallBackStatusData SecurityCheck()
        {
            if (UserService == null)
                return new CallBackStatusData { StatusCode = StatusCode.Fail, Message = "Not Find UserService" };

            return null;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [IComponentsMethod]
        public CallBackStatusData Login(string username, string password)
        {
            var user = UserService.GetUser(username);
            if (user != null && user.PassWord == password)
            {
                return new CallBackStatusData<string>
                {
                    StatusCode = StatusCode.Success,
                    Message = "登录成功",
                    Data = LoginManager.GetInstance().Login(user.UserId)
                };
            }

            return new CallBackStatusData { StatusCode = StatusCode.Fail, Message = "用户名或者密码错误" };
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [IComponentsMethod]
        public CallBackStatusData Logout()
        {
            var user = LoginManager.GetInstance().GetCurrentUser();

            if (user != null)
            {
                LoginManager.GetInstance().Logout(user.UserId);
            }

            return new CallBackStatusData { StatusCode = StatusCode.Success, Message = "登出成功" };
        }

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="old_password"></param>
        /// <param name="new_password"></param>
        /// <returns></returns>
        [IComponentsMethod]
        public CallBackStatusData ChangePassword(string old_password, string new_password)
        {
            var user = LoginManager.GetInstance().GetCurrentUser();

            if (user == null)
                return CallBackStatusDataEx.IdentityFailure();


            if (user.PassWord != old_password)
                return new CallBackStatusData { StatusCode = StatusCode.Fail, Message = "修改失败，原始密码不正确" };

            user.PassWord = new_password;
            UserService.UpdateUser(user);

            LoginManager.GetInstance().Logout(user.UserId);

            return new CallBackStatusData { StatusCode = StatusCode.Fail, Message = "密码修改成功，请使用新密码登录" };
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [IComponentsMethod]
        public CallBackStatusData GetUserInfo(long userid)
        {
            var user = UserService.GetUser(userid);

            if (user == null)
                return new CallBackStatusData { StatusCode = StatusCode.Fail, Message = "找不到的用户" };

            return new CallBackStatusData<User> { StatusCode = StatusCode.Success, Data = user };
        }
    }
}
